package com.paw.ruoyi.admin.common.context;

/**
 * 用户信息上下文
 *
 * @author Rubble
 * @date 2021/8/27
 **/
public class UserContextHolder {

  private static ThreadLocal<UserContext> local = ThreadLocal.withInitial(() -> new UserContext());


  public static void setUser (UserContext userContext) {
    local.set(userContext);
  }

  public static UserContext getUser () {
    return local.get();
  }

  public static void remove () {
    local.remove();
  }

  public static boolean hasUserContext () {
    UserContext userContext = getUser();
    return userContext != null && userContext.getUserId() != null;
  }


  public static Long getUserId () {
    return getUser() != null ? getUser().getUserId() : null;
  }

  public static String getUserName () {
    return getUser() != null ? getUser().getUserName() : null;
  }

}
